package com.ruoyi.information.controller.back;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.information.domain.back.SysSetBack;
import com.ruoyi.information.service.back.ISysSetBackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * V1.0配置Controller
 *
 * @author ruoyi
 * @date 2023-05-06
 */
@RestController
@RequestMapping("/setBack")
public class SysSetBackController extends BaseController {
    @Autowired
    private ISysSetBackService sysSetService;

    /**
     * 查询V1.0配置列表
     */
    @RequiresPermissions("information:set:list")
    @GetMapping("/list")
    public TableDataInfo list(SysSetBack sysSet) {
        startPage();
        List<SysSetBack> list = sysSetService.selectSysSetList(sysSet);
        return getDataTable(list);
    }

    /**
     * 导出V1.0配置列表
     */
    @RequiresPermissions("information:set:export")
    @Log(title = "V1.0配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysSetBack sysSet) {
        List<SysSetBack> list = sysSetService.selectSysSetList(sysSet);
        ExcelUtil<SysSetBack> util = new ExcelUtil<SysSetBack>(SysSetBack.class);
        util.exportExcel(response, list, "V1.0配置数据");
    }

    /**
     * 获取V1.0配置详细信息
     */
    @RequiresPermissions("information:set:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(sysSetService.selectSysSetById(id));
    }

    /**
     * 新增V1.0配置
     */
    @RequiresPermissions("information:set:add")
    @Log(title = "V1.0配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysSetBack sysSet) {
        /*banner验证*/
//        if (sysSet.getTitle() != null && sysSet.getSetType() != null && sysSet.getSetType() == 5) {
//            SysSet set = new SysSet();
//            set.setTitle(sysSet.getTitle());
//            List<SysSet> sysSets = sysSetService.selectSysSetList(set);
//            if (sysSets.size() > 0) {
//                return AjaxResult.error("每个位置只允许存在一条");
//            }
//        }
        return toAjax(sysSetService.insertSysSet(sysSet));
    }

    /**
     * 修改V1.0配置
     */
    @RequiresPermissions("information:set:edit")
    @Log(title = "V1.0配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysSetBack sysSet) {
        return toAjax(sysSetService.updateSysSet(sysSet));
    }

    /**
     * 删除V1.0配置
     */
    @RequiresPermissions("information:set:remove")
    @Log(title = "V1.0配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(sysSetService.deleteSysSetByIds(ids));
    }
}
